package org.edu.converter;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "convertDate")
public class ConvertDate implements Converter {

	SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");   
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,	String value) {
		df.setLenient(false);
        try {
            return df.parse(value);            
        } catch (Exception e) {
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro", null));
            
            throw new ConverterException( new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error2", null) ); 
        }
    }


	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		try {
			df.setLenient(false);
			String d = df.format((Date) value);
			return d;
		} catch (Exception e) {
			return "";
		}
	}

}
